package com.aca.market.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

@Configuration
public class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource dataSource(){
        return new DruidDataSource();
    }

    //后台监控 web.xml ServletRegistrationBean
    //springboot内置了Servlet容器 所以没有web.xml 作为替代:ServletRegistrationBean
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");

        // /druid/
        //后台登录 账号密码配置
        HashMap<String,String> initParameters = new HashMap<>();
        //key是固定的loginUsername和loginPassword
        initParameters.put("loginUsername","admin");
        initParameters.put("loginPassword","admin");
        //允许访问配置
        initParameters.put("allow","");
        //禁止访问配置
//        initParameters.put("xxx","192.168...")
        //初始化
        bean.setInitParameters(initParameters);
        return bean;
    }

    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        //过滤的请求
        HashMap<String,String> initParameters = new HashMap<>();
        //这些不统计 注意逗号
        initParameters.put("exclusions","*.js,*.css,/druid/*");
        return bean;
    }
}
